package com.yzt.minemodule.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.yzt.zhuju.arouter.RouterDynamic;
import com.yzt.zhuju.base.BaseFragment;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.game.GameBaseBean;
import com.yzt.zhuju.widget.refreshHeader.WaterDropHeader;
import com.yzt.minemodule.R;
import com.yzt.minemodule.adapter.UserActivityAdapter;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

/**
 * author : bob
 * date   : 2022/11/15
 * desc   : 用户活动
 */
public class UserActivityFragment extends BaseFragment implements OnRefreshListener {

    private UserActivityAdapter adapter;

    private SmartRefreshLayout mRefreshLayout;

    private String userId;

    public static UserActivityFragment getInstance(String userId) {
        Bundle args = new Bundle();
        args.putString("extra_user_id", userId);
        UserActivityFragment fragment = new UserActivityFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_user_info_activity;
    }

    @Override
    public void installViews() {
        userId = getArguments().getString("extra_user_id");
        RecyclerView rvRoomList = findViewById(R.id.list_rv);
        adapter = new UserActivityAdapter();
        rvRoomList.setLayoutManager(new GridLayoutManager(mActivity, 2));
        rvRoomList.setAdapter(adapter);

        adapter.getLoadMoreModule().setOnLoadMoreListener(() -> {
            mCurrentPage++;
            getPageData();
        });

        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter1, @NonNull View view, int position) {
                RouterDynamic.GameDetailActivity(adapter.getData().get(position).getActivityId() + "");
            }
        });

        mRefreshLayout = findViewById(R.id.refreshLayout);
        mRefreshLayout.setRefreshHeader(new WaterDropHeader(mActivity));
        mRefreshLayout.setOnRefreshListener(this);
        getPageData();
    }

    /**
     * 获取页面数据
     */
    private void getPageData() {

        BizLayer.getInstance().getIndexModule().getUserGameList(userId, mCurrentPage, PAGE_SIZE, new AresSubscriber<GameBaseBean>() {
            @Override
            protected void onSuccess(GameBaseBean mData) {
                if (mCurrentPage == 1) {
                    if (mData.getList().size() == 0) {
                        adapter.setEmptyView(getEmptyView());
                    } else {
                        adapter.removeEmptyView();
                    }
                    adapter.setList(mData.getList());
                } else {
                    adapter.addData(mData.getList());
                }
                adapter.getLoadMoreModule().loadMoreComplete();
                if (mData.getList().size() == 0) {
                    adapter.getLoadMoreModule().loadMoreEnd();
                }
            }

            @Override
            public boolean onErrorToast() {
                return true;
            }

            @Override
            protected void onFinallyEnd() {
                super.onFinallyEnd();
                mRefreshLayout.finishRefresh();
            }
        });
    }

    @Override
    public void netWorkRequest() {

    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        mCurrentPage = 1;
        getPageData();
    }
}

